package org.teachertools.web.actions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.data.Categorie;
import org.teachertools.services.CategorieService;
import org.teachertools.web.forms.CategorieForm;
import org.teachertools.web.forms.CategorieFormMapping;

public class CategorieAction extends CommonDispatchAction {

    @Override
    protected Map<String, String> getKeyMethodMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("button.save", "save");
        map.put("button.delete", "delete");
        map.put("button.search", "search");
        return map;
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        CategorieForm categoryForm = (CategorieForm) form;
        boolean isUpdate = categoryForm.getIsUpdate().booleanValue();
        CategorieService categorieService = this.getServiceLocator().getCategorieService();
        if (isUpdate) {
            Categorie loadedCategorie = categorieService.get(categoryForm.getId());
            Categorie categorie = CategorieFormMapping.mappingFormToDTO(categoryForm, loadedCategorie);
            categorieService.update(categorie);
        } else {
            Categorie categorie = CategorieFormMapping.mappingFormToDTO(categoryForm);
            categorieService.save(categorie);
        }
        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lenregistrement")));
        saveMessages(request, actionMessages);
        categoryForm.reset();
        addCategoryList(request);
        return mapping.findForward("successSave");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Long id = ((CategorieForm) form).getId();
        CategorieService categorieService = this.getServiceLocator().getCategorieService();
        categorieService.deleteById(id);
        ((CategorieForm) form).reset();
        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lasuppression")));
        saveMessages(request, actionMessages);

        addCategoryList(request);

        return mapping.findForward("successDelete");
    }

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Categorie categorie = CategorieFormMapping.mappingFormToDTO((CategorieForm) form);
        CategorieService categorieService = this.getServiceLocator().getCategorieService();
        List<Categorie> list = categorieService.searchByExample(categorie);
        request.setAttribute("RESULTS_LIST", list);
        return mapping.findForward("successSearch");
    }
}
